package com.wudl.file.controller;

import com.wudl.common.api.CommonResult;
import com.wudl.common.api.ResultCode;
import com.wudl.file.domain.FileInfo;
import com.wudl.file.domain.vo.fileUpLoadVo;
import com.wudl.file.service.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.http.fileupload.FileUpload;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;

/**
 * @author ：wudl
 * @date ：Created in 2022-03-17 18:29
 * @description： 文件操作接口
 * @modified By：
 * @version: 1.0
 */
@Slf4j
@RestController
@Api(tags = "文件上传")
@RequestMapping(value = "/file")

public class FileController {

    @Autowired
    FileService fileService;

    @GetMapping(value = "/getsuccess")
    public String getSuccess() {
        return "success";
    }

    @ApiOperation(value = "创建bucket", notes = "首先创建一个bucket")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bucket", value = "bucket名称", required = true, dataType = "String")
    })
    @GetMapping(value = "/createBucket")
    public CommonResult createBucket(@RequestParam("bucket") String bucket) {
        CommonResult bucket1 = fileService.createBucket(bucket);
        return bucket1;
    }


    /**
     * 上传一个文件
     *
     * @param stream
     * @param bucket
     * @param objectName
     */
    @PostMapping(value = "/uploadFileStream")
    public CommonResult uploadFile(@RequestParam("stream") InputStream stream, @RequestParam("bucket") String bucket, @RequestParam("objectName") String objectName) {

        CommonResult commonResult = fileService.uploadFile(stream, bucket, objectName);
        return commonResult;
    }

    /**
     * 上传一个文件
     *
     * @param fileUpLoadVo
     */
    @PostMapping(value = "/uploadFile")
    public CommonResult uploadFile(@RequestBody fileUpLoadVo fileUpLoadVo) {
        InputStream inStream = null;

        try {
            inStream = new FileInputStream(fileUpLoadVo.getSourcePaht());
        } catch (FileNotFoundException e) {
            log.info("获取文件流失败--------------------------->{}", e.getMessage());
            e.printStackTrace();
            return CommonResult.failed(ResultCode.FAILED);
        }
        CommonResult commonResult = fileService.uploadFile(inStream, fileUpLoadVo.getBucket(), fileUpLoadVo.getObjectName());
        return commonResult;
    }

    @RequestMapping(value = "/uploadfileMultipar", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult uploadfileMultipar(@RequestParam MultipartFile uploadfile, @RequestParam String bucket,
                                           @RequestParam(required = false) String objectName) throws Exception {
        fileService.createBucket(bucket);
        if (objectName != null) {
            fileService.uploadfileMultipar(uploadfile.getInputStream(), bucket, objectName + "/" + uploadfile.getOriginalFilename());
        } else {
            fileService.uploadfileMultipar(uploadfile.getInputStream(), bucket, uploadfile.getOriginalFilename());
        }
        return CommonResult.success("上传成功");
    }

    /**
     * 列出所有的桶
     *
     * @return
     */

    @GetMapping(value = "/listBuckets")
    public CommonResult<List<String>> listBuckets() {
        CommonResult<List<String>> listCommonResult = fileService.listBuckets();
        return listCommonResult;
    }

    /**
     * 列出一个桶中的所有文件和目录
     *
     * @param bucket
     * @return
     */
    @GetMapping(value = "/listFiles")
    public CommonResult<List<FileInfo>> listFiles(@RequestParam("bucket") String bucket) {
        CommonResult<List<FileInfo>> listCommonResult = fileService.listFiles(bucket);
        return listCommonResult;
    }


    /**
     * 下载一个文件
     *
     * @param bucket
     * @param objectName
     * @return
     */
    @GetMapping(value = "/download")
    public CommonResult<InputStream> download(@RequestParam("bucket") String bucket, @RequestParam("objectName") String objectName) {
        CommonResult<InputStream> download = fileService.download(bucket, objectName);
        return download;
    }

    @RequestMapping(value = "/downloadFile", method = RequestMethod.GET)
    @ResponseBody
    public void downloadFile(@RequestParam String bucket, @RequestParam String objectName,
                             HttpServletResponse response) throws Exception {
        InputStream stream = fileService.downloadFile(bucket, objectName);
        ServletOutputStream output = response.getOutputStream();
        response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(objectName.substring(objectName.lastIndexOf("/") + 1), "UTF-8"));
        response.setContentType("application/octet-stream");
        response.setCharacterEncoding("UTF-8");
        IOUtils.copy(stream, output);
    }

    /**
     * 删除一个桶
     *
     * @param bucket
     */
    @GetMapping(value = "/deleteBucket")
    public CommonResult deleteBucket(@RequestParam("bucket") String bucket) {
        CommonResult commonResult = fileService.deleteBucket(bucket);
        return commonResult;
    }

    /**
     * 删除一个对象
     *
     * @param bucket
     * @param objectName
     */
    @GetMapping(value = "/deleteObject")
    public CommonResult deleteObject(@RequestParam("bucket") String bucket, @RequestParam("objectName") String objectName) {
        CommonResult commonResult = fileService.deleteObject(bucket, objectName);
        return commonResult;
    }


}
